package com.wh.springboot.tengxunapi;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


public class TestApi {
    public static void main(String[] args) throws Exception {
        HttpUtilss httpUtils = new HttpUtilss();
        String string = httpUtils.getData();
        System.out.println("原始数据:" + string);


        //1.所有数据的alibaba的格式
        //① str转化为相应的JSONObject对象
        JSONObject jsonObject = JSONObject.parseObject(string);
        //② 根据键值来获取数据
        Object data = jsonObject.get("data");
        System.out.println("Data:" + data);

        //2.data 地区数据
        JSONObject jsonObjectData = JSONObject.parseObject(data.toString());

        //拿到areaTree
        JSONArray areaTree = jsonObjectData.getJSONArray("areaTree");
        System.out.println("AreaTree:" + areaTree);
        Object[] objects = areaTree.toArray();
        //遍历所有国家的数据
//        for (int i = 0; i < objects.length; i++) {
//            JSONObject jsonObjectArray = JSONObject.parseObject(objects[i].toString());
//            Object name = jsonObjectArray.get("name");
//            System.out.println("Name:" + name);
//
//        }

        //3.中国地区
        JSONObject jsonObjectChinaData = JSONObject.parseObject(objects[2].toString());
        JSONArray children = jsonObjectChinaData.getJSONArray("children");

        Object[] chinaprovinceobjects = children.toArray();
        for (int i = 0; i < chinaprovinceobjects.length; i++) {
            JSONObject jsonObjectArray = JSONObject.parseObject(chinaprovinceobjects[i].toString());
            Object provincename = jsonObjectArray.get("name");//省份
            Object provincelastUpdateTime = jsonObjectArray.get("lastUpdateTime");//省份更新数据时间
            Object total = jsonObjectArray.get("total");
            JSONObject jsonObjectChinaProvinceData = JSONObject.parseObject(total.toString());//total
            Object provinceconfirm = jsonObjectChinaProvinceData.get("confirm");//省份确诊病例
            System.out.println("省份->"+provincename+" 确诊人数:"+provinceconfirm);
        }


    }

    private static String getType(Object a) {
        return a.getClass().toString();
    }
}
